﻿using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System.Collections.Generic;
using System.Linq;

namespace AutoCAD_CSharp_plug_in3.Chap04
{
    public static class Block4
    {
        private static readonly Dictionary<string, double[]> Dn = new Dictionary<string, double[]>
        {
            // 格式: A, D, N, C1, C2, H1, H2
            { "DN32", new double[] { 42, 140, 56, 18, 18, 42, 42 } },
            { "DN40", new double[] { 48, 150, 64, 18, 18, 45, 45 } },
            { "DN50", new double[] { 60, 165, 75, 20, 20, 48, 48 } },
            { "DN65", new double[] { 76, 185, 90, 22, 22, 52, 52 } },
            { "DN80", new double[] { 89, 200, 105, 24, 24, 58, 58 } },
            { "DN100", new double[] { 114, 235, 134, 24, 24, 65, 65 } },
            { "DN125", new double[] { 140, 270, 162, 26, 26, 68, 68 } },
            { "DN100✳", new double[] { 114, 235, 134, 28, 28, 69, 69 } },
            { "DN125✳", new double[] { 140, 270, 162, 30, 30, 72, 72 } },
            { "DN150", new double[] { 168, 300, 192, 32, 32, 79, 79 } }
        };

        private static double[] GetDnVal(string key)
        {
            return Dn.TryGetValue(key, out var val) ? val : Dn.Values.First();
        }

        private static BlockTableRecord Create(out string blockName, string dn)
        {
            blockName = "法兰PN40系列-主视" + "_" + dn;
            var blockDef = new BlockTableRecord
            {
                Name = blockName,
                Origin = Point3d.Origin
            };

            var dnVal = GetDnVal(dn);
            var a = dnVal[0];
            var d = dnVal[1];
            var n = dnVal[2];
            var c1 = dnVal[3];
            var c2 = dnVal[4];
            var h1 = dnVal[5];
            var h2 = dnVal[6];

            var po = Point3d.Origin;

            var p1 = new Point3d(po.X - h1, po.Y, 0);
            var p2 = new Point3d(po.X + h2, po.Y, 0);
            var t0 = new Point3d(po.X, po.Y + d / 2, 0);
            var e0 = new Point3d(po.X, po.Y - d / 2, 0);
            var t1 = new Point3d(po.X - c1, po.Y + d / 2, 0);
            var e1 = new Point3d(po.X - c1, po.Y - d / 2, 0);
            var t2 = new Point3d(po.X + c2, po.Y + d / 2, 0);
            var e2 = new Point3d(po.X + c2, po.Y - d / 2, 0);
            var t3 = new Point3d(po.X - c1, po.Y + n / 2, 0);
            var e3 = new Point3d(po.X - c1, po.Y - n / 2, 0);
            var t4 = new Point3d(po.X + c2, po.Y + n / 2, 0);
            var e4 = new Point3d(po.X + c2, po.Y - n / 2, 0);
            var t5 = new Point3d(po.X - h1, po.Y + a / 2, 0);
            var e5 = new Point3d(po.X - h1, po.Y - a / 2, 0);
            var t6 = new Point3d(po.X + h2, po.Y + a / 2, 0);
            var e6 = new Point3d(po.X + h2, po.Y - a / 2, 0);


            var pol = new Line(p1, p2)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 4)
            };

            var te0 = new Line(t0, e0)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var te1 = new Line(t1, e1)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var te2 = new Line(t2, e2)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var t12 = new Line(t1, t2)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var e12 = new Line(e1, e2)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var t35 = new Line(t3, t5)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var e35 = new Line(e3, e5)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var t46 = new Line(t4, t6)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var e46 = new Line(e4, e6)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var te5 = new Line(t5, e5)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            var te6 = new Line(t6, e6)
            {
                Color = Color.FromColorIndex(ColorMethod.ByAci, 256)
            };

            blockDef.AppendEntity(pol);
            blockDef.AppendEntity(te0);
            blockDef.AppendEntity(te1);
            blockDef.AppendEntity(te2);
            blockDef.AppendEntity(t12);
            blockDef.AppendEntity(e12);
            blockDef.AppendEntity(t35);
            blockDef.AppendEntity(e35);
            blockDef.AppendEntity(t46);
            blockDef.AppendEntity(e46);
            blockDef.AppendEntity(te5);
            blockDef.AppendEntity(te6);

            return blockDef;
        }

        public static List<BlockTableRecord> CreateAll(string blockNamePrefix)
        {
            var blocks = new List<BlockTableRecord>();
            foreach (var pair in Dn)
            {
                var record = Create(out var blockName, pair.Key);
                record.Name = blockNamePrefix + blockName;

                blocks.Add(record);
            }

            return blocks;
        }
    }
}
